package use;

import java.io.*;
import java.util.Scanner;

public class Register {//注册

    public void regist() throws IOException {
        WorkSystem workSystem = new WorkSystem();
        Scanner sc = new Scanner(System.in);
        String[] str = new String[2];
        System.out.println("请输入用户名:");
        str[0] = sc.next();
        System.out.println("请输入密码:");
        str[1] = sc.next();
        System.out.println("请再次输入密码:");
        String repassword = sc.next();
        if (str[1].equals(repassword)) {
            FileReader fr = new FileReader("information\\customer.txt");
            BufferedReader bfr = new BufferedReader(fr);
            String user;
            int reflag = 0;
            while ((user = bfr.readLine()) != null) {
                String[] sr = user.split(",");
                if (sr[0].equals(str[0]) && sr[1].equals(str[1])) {
                    System.out.println("您已注册！");
                    workSystem.welcome();
                    reflag = 1;
                    break;
                }
            }
            if (reflag == 0) {
                System.out.println("恭喜您，注册成功！");
                FileWriter fw = new FileWriter("information\\customer.txt", true);
                int i;
                for (i = 0; i < str.length - 1; i++) {
                    fw.write(str[i] + ",");
                }
                fw.write(str[i]);
                fw.write("\r\n");
                fw.close();
                workSystem.welcome();
            }

        } else {
            System.out.println("注册失败!");
            int n = 0;
            while (n == 0) {
                System.out.println("请选择" + "\n" + "1、重新注册" + "2、退出系统");
                String newUser = sc.next();
                switch (newUser) {
                    case "1":
                        System.out.println("请输入用户名:");
                        String name = sc.next();
                        System.out.println("请输入密码:");
                        String password = sc.next();
                        System.out.println("请再次输入密码:");
                        String samePassword = sc.next();
                        if (password.equals(samePassword)) {
                            FileReader fileReader = new FileReader("information\\customer.txt");
                            BufferedReader bufferedReader = new BufferedReader(fileReader);
                            String user;
                            int flag = 0;
                            while ((user = bufferedReader.readLine()) != null) {
                                String[] resr = user.split(",");
                                if (resr[0].equals(name) && resr[1].equals(password)) {
                                    System.out.println("您已注册!");
                                    workSystem.welcome();
                                    flag = 1;
                                    break;
                                }
                            }
                            if (flag == 0) {
                                System.out.println("恭喜您，注册成功！");
                                FileWriter fileWriter = new FileWriter("information\\customer.txt", true);
                                fileWriter.write("\r\n");
                                fileWriter.write(name + "," + password);
                                fileWriter.close();
                                n = 1;
                            }

                        }
                        break;
                    case "2":
                        System.out.println("您已退出系统，欢迎下次使用！");
                        break;
                }
            }
        }
    }
}
